package com.ruoyi.cms.service.impl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.cms.domain.CmsSiteBanne;
import com.ruoyi.cms.domain.CmsSiteCase;
import com.ruoyi.cms.domain.CmsSiteGood;
import com.ruoyi.cms.domain.vo.CmsSiteAboutVo;
import com.ruoyi.cms.domain.vo.CmsSiteCaseVo;
import com.ruoyi.cms.service.ICmsSiteBanneService;
import com.ruoyi.cms.service.ICmsSiteGoodService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cms.mapper.CmsSiteAboutMapper;
import com.ruoyi.cms.domain.CmsSiteAbout;
import com.ruoyi.cms.service.ICmsSiteAboutService;

/**
 * 关于我们-模块信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-09-07
 */
@Service
public class CmsSiteAboutServiceImpl implements ICmsSiteAboutService 
{
    @Autowired(required = false)
    private CmsSiteAboutMapper cmsSiteAboutMapper;
    @Autowired(required = false)
    private ICmsSiteGoodService cmsSiteGoodService;

    /**
     * 查询关于我们-模块信息
     * 
     * @param id 关于我们-模块信息主键
     * @return 关于我们-模块信息
     */
    @Override
    public CmsSiteAbout selectCmsSiteAboutById(Long id)
    {
        return cmsSiteAboutMapper.selectCmsSiteAboutById(id);
    }

    /**
     * 查询关于我们-模块信息列表
     * 
     * @param cmsSiteAbout 关于我们-模块信息
     * @return 关于我们-模块信息
     */
    @Override
    public List<CmsSiteAbout> selectCmsSiteAboutList(CmsSiteAbout cmsSiteAbout)
    {
        return cmsSiteAboutMapper.selectCmsSiteAboutList(cmsSiteAbout);
    }

    /**
     * 新增关于我们-模块信息
     * 
     * @param cmsSiteAbout 关于我们-模块信息
     * @return 结果
     */
    @Override
    public int insertCmsSiteAbout(CmsSiteAbout cmsSiteAbout)
    {
        return cmsSiteAboutMapper.insertCmsSiteAbout(cmsSiteAbout);
    }

    /**
     * 修改关于我们-模块信息
     * 
     * @param cmsSiteAbout 关于我们-模块信息
     * @return 结果
     */
    @Override
    public int updateCmsSiteAbout(CmsSiteAbout cmsSiteAbout)
    {
        return cmsSiteAboutMapper.updateCmsSiteAbout(cmsSiteAbout);
    }

    /**
     * 批量删除关于我们-模块信息
     * 
     * @param ids 需要删除的关于我们-模块信息主键
     * @return 结果
     */
    @Override
    public int deleteCmsSiteAboutByIds(Long[] ids)
    {
        return cmsSiteAboutMapper.deleteCmsSiteAboutByIds(ids);
    }

    /**
     * 删除关于我们-模块信息信息
     * 
     * @param id 关于我们-模块信息主键
     * @return 结果
     */
    @Override
    public int deleteCmsSiteAboutById(Long id)
    {
        return cmsSiteAboutMapper.deleteCmsSiteAboutById(id);
    }


    @Override
    public List<CmsSiteAboutVo> getAllInfo() {
        List<CmsSiteAbout> cmsSiteAbouts = this.selectCmsSiteAboutList(null);
        List<CmsSiteGood> cmsSiteGoods = cmsSiteGoodService.selectCmsSiteGoodList(null);

        //封装数据
        List<CmsSiteAboutVo> collect = cmsSiteAbouts.stream().map(module -> {
            CmsSiteAboutVo cmsSiteWebsitVo = new CmsSiteAboutVo();
            BeanUtils.copyProperties(module, cmsSiteWebsitVo);
            List<CmsSiteGood> cmsSiteBanners1 = new ArrayList<>();
            cmsSiteGoods.stream().forEach(banner -> {
                if (module.getId().equals(banner.getAboutId())) {
                    cmsSiteBanners1.add(banner);
                }
            });
            //放之前排序
            cmsSiteBanners1.sort(Comparator.comparing(CmsSiteGood::getSort));
            cmsSiteWebsitVo.setCmsSiteGoods(cmsSiteBanners1);
            return cmsSiteWebsitVo;
        }).collect(Collectors.toList());

        //返回前排序
        collect.sort(Comparator.comparing(CmsSiteAboutVo::getSort));

        return collect;
    }
}
